package com.example.qxfw.slhx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.zy.HxyjWarning;
import com.example.qxfw.common.util.DateUtil;
import com.example.qxfw.mongodb.hy.entity.HyDocFile;
import com.example.qxfw.mongodb.hy.service.DocFileService;
import com.example.qxfw.slhx.service.HxlsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.List;

@Api(tags = "森林火险--火险历史")
@CrossOrigin
@RestController
@RequestMapping("FireInsuranceHistoryController")
public class FireInsuranceHistoryController {

    @Autowired
    private HxlsService hxlsService;

    @Autowired
    private DocFileService docFileService;

    @ApiOperation("森林火险--火险历史-查询预警历史")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startTime", value = "yyyy-MM-dd HH:mm:ss", dataType = "String", required = true),
            @ApiImplicitParam(name = "endTime", value = "yyyy-MM-dd HH:mm:ss", dataType = "String", required = true),
            @ApiImplicitParam(name = "state", value = "状态，暂时默认传2", dataType = "String", required = true)
    })
    @GetMapping("/getAllOfHxyjWarning")
    public SysResult getAllOfHxyjWarning(String startTime, String endTime, Integer state){
        Timestamp startDate = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(startTime));
        Timestamp endDate = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(endTime));
        List<HxyjWarning> list = hxlsService.getAllOfHxyjWarning(startDate, endDate, state);
        return SysResult.success(list);
    }

    @ApiIgnore
    @ApiOperation("森林火险--火险历史-更新预警状态")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "docId", value = "产品ID", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "state", value = "审核状态", dataType = "Integer", required = true)
    })
    @GetMapping("/updateState")
    public SysResult updateState(Integer docId, Integer state){
        HxyjWarning hxyjWarning = hxlsService.updateState(docId, state);
        return SysResult.success(hxyjWarning);
    }

    @ApiOperation("森林火险--火险历史-查询文件id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "docId", value = "产品ID", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "docPath", value = "产品名称", dataType = "String", required = true)
    })
    @GetMapping("/getProductId")
    public SysResult getProductId(Integer docId,String docPath){
        Integer productId = hxlsService.getProductId(docId, docPath);
        return SysResult.success(productId);
    }


    /**
     * 得到产品
     * @param id
     * @param response
     * @throws IOException
     * @throws ParseException
     */
    @ApiOperation("根据id得到文档")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "文件id",dataType = "String",required = true)
    })
    @PostMapping("/getDocById")
    public void getDocById(String id, HttpServletResponse response) throws IOException, ParseException {
        HyDocFile pdfFile = docFileService.getByTableId(id);
        if (pdfFile != null) {
            InputStream inputStream = new ByteArrayInputStream(pdfFile.getData());
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("argiproduct_" + id, "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = inputStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
            inputStream.close();
        } else {
            System.out.println("文件不存在");
        }
    }

    @ApiOperation("森林火险--火险历史-删除预警信息")
    @GetMapping("/deleteByIdOfHxyjWarning")
    public SysResult deleteByIdOfHxyjWarning(int docId){
        try {
            int result = hxlsService.deleteByIdOfHxyjWarning(docId);
            return SysResult.success(result);
        } catch (Exception e){
            System.err.println("删除错误");
            return SysResult.fail("删除错误");
        }
    }


}
